<template>
  <dt-modal-edit ref="edit" :label-width="160" :title="operateType?'甲方业绩确认证明修改':'甲方业绩确认证明新增'" @ok="onOk">
    
  </dt-modal-edit>
</template>

<script>
  import proveMgr from '@/biz/caseReport/ownerProve.js'
  import createOpt from './create'
  import moment from 'moment'
  export default {
    data() {
      return {
        info:{
          buildingId: this.$dt.buildInfo().housesId,
          confirmDate: '',
          flag: '',
          isSend:'false',
          noSendExplain:'',
          salesAmount: '',
          expressNo: '',
          appendix: [],
          remarks: '',
          attachType: ''
        },
        operateType:0, //0新增 1修改
        dateArr:[]
      }
    },
    methods: {
      // 处理业绩确认周期数据
      resolveConfirmDate(){
        let curYear = moment().year()
        let preYear = curYear - 1
        let curQuarter = moment().quarter()
        this.dateArr = []
        // 当前年
        for(let i=curQuarter;i>=1;i--){
          let obj  = this.tempFun(curYear,i)
          this.dateArr.push(obj)
        }

        // 前一年
        for(let i=4;i>=1;i--){
          let obj  = this.tempFun(preYear,i)
          this.dateArr.push(obj)
        }
      },
      tempFun(year,index){
        let tempArr = [{
          key:`${year}-10/${year}-12`,
          value:`${year}年第四季度 (${year}年10月-${year}年12月)`,
          tag:4
        },{
          key:`${year}-07/${year}-09`,
          value:`${year}年第三季度 (${year}年7月-${year}年9月)`,
          tag:3
        },{
          key:`${year}-04/${year}-06`,
          value:`${year}年第二季度 (${year}年4月-${year}年6月)`,
          tag:2
        },{
          key:`${year}-01/${year}-03`,
          value:`${year}年第一季度 (${year}年1月-${year}年3月)`,
          tag:1
        }]

        let obj = tempArr.filter(item=>item.tag == index)[0]
        return obj
      },
      show(info){
        this.resolveConfirmDate()

        if (Object.keys(info).length) {
          if(!this.dateArr.filter(item=>item.key==info.confirmDate).length){
            info.confirmDate = ''
          }
          this.info = {...info}
          this.operateType = 1
        } else {
          this.operateType = 0
          this.info = {
            buildingId: this.$dt.buildInfo().housesId,
            confirmDate: '',
            flag: '',
            isSend:'false',
            noSendExplain:'',
            salesAmount: '',
            expressNo: '',
            appendix: [],
            remarks: '',
            attachType: ''
          }
        }
        this.$refs.edit.show(new createOpt(this).options(), this.info)
      },

      // 设置利率值
      onOk() {
        let params = {
          ...this.info
        }

        params.appendix =  this.info.appendix.length? JSON.stringify(this.info.appendix):''
        params.confirmStart = moment(params.confirmDate.split('/')[0]).startOf("month").format("YYYY-MM-DD")
        params.confirmEnd =  moment(params.confirmDate.split('/')[1]).endOf("month").format("YYYY-MM-DD")

        if(this.operateType){
          proveMgr.update({
            ...params,
            updateId: this.$dt.userInfo().userId,
            userName: this.$dt.userInfo().userName
          }).then(() => {
            this.$Message.success('修改成功')
            this.$emit('onSearch')
            this.$refs.edit.hide()
          }).catch(err => {
          })
        }else{
          proveMgr.add(params).then(() => {
            this.$Message.success('新增成功')
            this.$emit('onSearch')
            this.$refs.edit.hide()
          }).catch(err => {
          })
        }
      }
    },
  }
</script>

<style lang='scss' scoped>
  .confirm-images {
    display: flex;
    flex-wrap: wrap;

    p {
      margin-right: 12px;
      display: flex;
      align-items: center;
    }

    span {
      color: #3366ff;
      text-decoration: underline;
      cursor: pointer;
    }

    i {
      cursor: pointer;
      margin-top: 2px;
    }
  }
</style>